import { getNav, metaGetNav } from '@/api/login'
// import { constantRouterMap } from '@/router'
const app = {
  state: {
    navOpen: true, // 默认侧边功能栏是否展开
    path: '',
    title: '',
    icon: '',
    navBar: [],
    logoBarStatus: true // 集成系统为false
    // 无获取菜单接口或接口报错时, 注释上句, 打开下句, 并将state中path和title修改为接口对应字段即可
    // navBar: constantRouterMap
  },
  mutations: {
    SET_NAVOPEN: state => {
      state.navOpen = !state.navOpen // 改变侧边栏收缩/张开
    }
  },
  actions: {
    // 获取nav
    GetNav ({ commit }, token) {
      return new Promise((resolve, reject) => {
        getNav(token)
          .then(response => {
            const data = response.data
            if (data.success) {
              commit('SET_NAVBAR', data.msg.reverse())
              resolve(data.data)
            } else {
              // eslint-disable-next-line prefer-promise-reject-errors
              reject()
            }
          })
          .catch(error => {
            reject(error)
          })
      })
    },
    metaGetNav ({ commit }, token) {
      return new Promise((resolve, reject) => {
        metaGetNav(token)
          .then(response => {
            const data = response.data
            if (data.success) {
              commit('SET_NAVBAR', data.msg.reverse())
              resolve(data.data)
            } else {
              // eslint-disable-next-line prefer-promise-reject-errors
              reject()
            }
          })
          .catch(error => {
            reject(error)
          })
      })
    }
  }
}

export default app
